package com.itlong.cloud.excpsms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.excpsms.service.INotifyDevelopersService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.sms.SmsMsgService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <desc>
 *      短信通知开发者有异常发生
 * </desc>
 *
 * @createDate 2020/11/16
 */
@Service
public class NotifyDevelopersServiceImpl implements INotifyDevelopersService {
    private static final Logger LOG = LoggerFactory.getLogger(NotifyDevelopersServiceImpl.class);
    @Value("${" + PlatformConstants.EXCEPTION_NOTIFY_DEVELOPER_TELE_NUMS + "}")
    public String EXCEPTION_NOTIFY_DEVELOPER_TELE_NUMS;
    @Autowired
    RedisService redisService;
    @Autowired
    private SmsMsgService smsMsgService;
    /**
     * <desc>
     *      短信通知异常信息
     * </desc>
     *
     * @param  msg   错误信息
     * @return
     * @author pengwei
     * @createDate 2020/11/16
     */
    @Override
    public     void   notify( String msg)  {
          boolean canSend   =  canSend();
          if(canSend){
               //拿到开发者的手机号码
              if(StringUtils.isNotEmpty(EXCEPTION_NOTIFY_DEVELOPER_TELE_NUMS)){
                      String[]   teleNums  =   EXCEPTION_NOTIFY_DEVELOPER_TELE_NUMS.split(",");
                      for(String telNum:teleNums){
                          boolean  sendFlag  = smsMsgService.sendSmsCodeByPhone(telNum,msg);
                          if(!sendFlag){
                              LOG.info(new StringBuilder("手机号码").append(teleNums).append("发送短信失败！").toString());
                              break;
                          }
                      }
              }
          }
    }

    /**
     * <desc>
     *      是否可以发送
     *      1h之内只能发送3次
     * </desc>
     * @author pengwei
     * @createDate 2020/11/16
     */
    private   boolean  canSend(){
        String   exceptionConfig =   redisService.get(RedisConstant.DATA_ACCESS_EXCEPTION_NOTIFY_DEVLOPS_CACHE_KEY);
        long  currTimestamp  =  System.currentTimeMillis();
        boolean   canSendSms  =   false;
        JSONObject configJson =  null;
        if(exceptionConfig!=null&&exceptionConfig.length()>0){
            configJson  =   JSONObject.parseObject(exceptionConfig);
            //计时器开始时间
            long   startTime   =   configJson.getLong("startTime");
            //已发送次数
            int    count       =   configJson.getIntValue("count");
            long   gap         =   currTimestamp-startTime;
            long   compareTimeOneHour =   60*60*1000;
            if(gap<=compareTimeOneHour){
                if(count<3){
                    canSendSms  =true;
                    configJson.put("count",++count);
                }
            }else{
                configJson.put("startTime",currTimestamp);
                configJson.put("count",1);
                canSendSms  = true;
            }
        }else{
            configJson  = new JSONObject();
            configJson.put("startTime",currTimestamp);
            configJson.put("count",1);
            canSendSms  = true;
        }
        redisService.set(RedisConstant.DATA_ACCESS_EXCEPTION_NOTIFY_DEVLOPS_CACHE_KEY,configJson.toJSONString());
        return canSendSms;
    }
}
